package org.csu.mypetstore.controller;

import org.apache.commons.lang3.math.NumberUtils;
import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.domain.Item;
import org.csu.mypetstore.domain.Order;
import org.csu.mypetstore.domain.Product;
import org.csu.mypetstore.persistence.OrderMapper;
import org.csu.mypetstore.service.AccountService;
import org.csu.mypetstore.service.AdminService;
import org.csu.mypetstore.service.CartService;
import org.csu.mypetstore.service.CatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/adminCatalog")
public class AdminCatalogController {
    @Autowired
    CatalogService catalogService;
    @Autowired
    AccountService accountService;
    @Autowired
    AdminService adminService;
    @Autowired
    CartService cartService;


    @GetMapping("/viewEditCategory")
    public String viewEditCategory(Model model){
        //鱼列表
        List<Product> fishProductList = catalogService.getProductListByCategory("FISH");
        model.addAttribute("fishProductList",fishProductList);
        //鸟列表
        List<Product> birdProductList = catalogService.getProductListByCategory("BIRDS");
        model.addAttribute("birdProductList",birdProductList);
        //猫列表
        List<Product> catProductList = catalogService.getProductListByCategory("CATS");
        model.addAttribute("catProductList",catProductList);
        //狗列表
        List<Product> dogProductList = catalogService.getProductListByCategory("DOGS");
        model.addAttribute("dogProductList",dogProductList);
        //爬行动物列表
        List<Product> reptileProductList = catalogService.getProductListByCategory("REPTILES");
        model.addAttribute("reptileProductList",reptileProductList);

        return "EditItem/EditCategory";
    }

    @GetMapping("/viewEditProduct")
    public String viewEditProduct(String productId, Model model){
        if(productId != null){
            Product product = catalogService.getProduct(productId);
            List<Item> itemList = catalogService.getItemListByProduct(productId);
            model.addAttribute("product",product);
            model.addAttribute("itemList",itemList);
            return "EditItem/EditProduct";
        }
        return "catalog/Adminmain";
    }

    @GetMapping("/viewEditItem")
    public String viewEditItem(String itemId,Model model,String productId)
    {
        if(itemId!=null)
        {
            Item item = catalogService.getItem(itemId);
            Product product = catalogService.getProduct(productId);
            model.addAttribute("item",item);
            model.addAttribute("product",product);

            return "EditItem/EditItem";
        }
        return "catalog/Adminmain";
    }

    //查看所有的用户
    @GetMapping("/viewEditUserInfo")
    public String viewEditUser(Model model)
    {
        List <Account> userList=accountService.getAccountList();
        model.addAttribute("userList",userList);
        return  "EditUser/EditUserInfo";
    }

    //查看所有未处理的订单
    @GetMapping("/viewEditOrder")
    public String viewEditOrder(Model model){

        List<Order> orderList = adminService.getUnhandledOrders();

        model.addAttribute("orderList",orderList);

        return "/EditOrder/EditOrder";
    }


    //储存Item的修改信息
    @PostMapping("/adminfind")
    public String  adminfind(HttpServletRequest req)
    {
        String adminitemid=null;
        String admindescription=null;
        BigDecimal adminlistprice=null;
        adminitemid=req.getParameter("adminitemid");
        admindescription=req.getParameter("admindescription");
        adminlistprice= NumberUtils.createBigDecimal(req.getParameter("adminlistprice"));
        cartService.updateadmindesandprice(adminitemid,admindescription,adminlistprice);
        return "catalog/Adminmain";
    }

    //跳转到新增Item页面
    @GetMapping("/viewAddItem")
    public String addItem(Model model, String productId){
        List<String> productIdList = catalogService.getProductIdList();
        boolean haveProduct = true;
        if (productId == null){
            haveProduct = false;
        }

        model.addAttribute("haveProduct",haveProduct);
        model.addAttribute("productList",productIdList);
        model.addAttribute("productId",productId);

        return "EditItem/AddItem";
    }


    //新增商品
    @PostMapping("/addItem")
    public String viewAddItem(HttpServletRequest req, Model model,String productId){
        Item item = new Item();
        boolean haveProduct = false;
        if (productId!=null){
            haveProduct = true;
        }

        item.setItemId(req.getParameter("itemId"));
        item.setProductId(req.getParameter("productId"));
        item.setListPrice(NumberUtils.createBigDecimal(req.getParameter("listPrice")));
        item.setAttribute1(req.getParameter("attr1"));
        item.setAttribute2(req.getParameter("attr2"));
        item.setAttribute3(req.getParameter("attr3"));
        item.setAttribute4(req.getParameter("attr4"));
        item.setSupplierId(Integer.parseInt(req.getParameter("supplier")));
        item.setQuantity(Integer.parseInt(req.getParameter("qty")));

        adminService.addItem(item);

        List<String> productIdList = catalogService.getProductIdList();

        model.addAttribute("haveProduct",haveProduct);
        model.addAttribute("productList",productIdList);
        model.addAttribute("productId",productId);

        return "EditItem/AddItem";
    }

    //删除商品
    @GetMapping("/admindelete")
    public String admindelete(String itemId){
        adminService.admindelete(itemId);
        System.out.println(itemId);
        return "catalog/Adminmain";
    }

    //跳转到增加Product的页面
    @GetMapping("/viewAddProduct")
    public String viewAddProduct(){
        return "EditItem/AddProduct";
    }

    // 增加Product
    @PostMapping("/addProduct")
    public String addProduct(@RequestParam(value = "file") MultipartFile file, Model model, HttpServletRequest req){
        Product product = new Product();
        product.setProductId(req.getParameter("productId"));
        product.setCategoryId(req.getParameter("category"));
        product.setName(req.getParameter("name"));

        if (file.isEmpty()) {
            System.out.println("文件为空空");
        }
        String fileName = file.getOriginalFilename();  // 文件名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));  // 后缀名
        String filePath = "E://GitBase//mypetstore-pro-2//src//main//resources//static//images//"; // 上传后的路径
        fileName = UUID.randomUUID() + suffixName; // 新文件名
        File dest = new File(filePath + fileName);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String filename = "images/" + fileName;

        product.setDescription(filename);
        product.setDescriptionWords(req.getParameter("descnword"));

        adminService.addProduct(product);

        return "EditItem/AddProduct";

    }



}
